package Eyjafjalla.action;

import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.vfx.combat.FlashAtkImgEffect;

import java.util.ArrayList;
import java.util.Iterator;

public class RecitativeAction extends AbstractGameAction {
    public RecitativeAction(AbstractCreature target,int amount,int block){
        this.target =target;
        this.amount = amount;
        this.block = block;
        this.actionType = ActionType.BLOCK;
        this.duration = 0.25F;
        this.startDuration = 0.25F;
    }

    int block;

    @Override
    public void update() {
        if(this.duration == this.startDuration&&!this.target.isDying && !this.target.isDead ) {
            ArrayList<AbstractCard> burns = new ArrayList<>();
            Iterator var1;
            AbstractCard c;
            var1 = AbstractDungeon.player.drawPile.group.iterator();
            while(var1.hasNext()) {
                c = (AbstractCard)var1.next();
                if (EyjafjallaHelper.isBurn(c)) {
                    burns.add(c);
                }

            }
            var1 = AbstractDungeon.player.discardPile.group.iterator();
            while(var1.hasNext()) {
                c = (AbstractCard)var1.next();
                if (EyjafjallaHelper.isBurn(c)) {
                    burns.add(c);
                }
            }
            //从大到小排序
            burns.sort((b1,b2)-> {
                int cb1 = EyjafjallaHelper.getUpgrade(b1);
                int cb2 = EyjafjallaHelper.getUpgrade(b2);
                return Integer.compare(cb2, cb1);
            });

            int tmpBlock = 0;
            for(int i =0;i<burns.size()&&i<this.amount;i++){
                tmpBlock+=EyjafjallaHelper.getUpgrade(burns.get(i))*block;
                if(AbstractDungeon.player.drawPile.contains(burns.get(i))){
                    AbstractDungeon.player.drawPile.moveToHand(burns.get(i));
                }
                else if(AbstractDungeon.player.discardPile.contains(burns.get(i))){
                    AbstractDungeon.player.discardPile.moveToHand(burns.get(i));
                    burns.get(i).current_x = CardGroup.DISCARD_PILE_X;
                }
            }

            AbstractDungeon.effectList.add(new FlashAtkImgEffect(this.target.hb.cX, this.target.hb.cY, AttackEffect.SHIELD));
            this.target.addBlock(tmpBlock);

            AbstractDungeon.player.hand.applyPowers();
        }

        this.tickDuration();
    }
}
